package com.poetry.repository;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.poetry.model.Poem;

@Repository
public interface PoemRepository extends JpaRepository<Poem, Long>, JpaSpecificationExecutor<Poem> {
    List<Poem> findByAuthorIdOrderByCreatedAtDesc(Long authorId);
    
    @Query("SELECT p FROM Poem p JOIN p.likedByUsers u WHERE u.id = ?1 ORDER BY p.createdAt DESC")
    List<Poem> findLikedPoemsByUserId(Long userId);
    
    @Query("SELECT p FROM Poem p JOIN p.bookmarkedByUsers u WHERE u.id = ?1 ORDER BY p.createdAt DESC")
    List<Poem> findBookmarkedPoemsByUserId(Long userId);
    
    Page<Poem> findByTitleContainingOrContentContaining(String title, String content, Pageable pageable);
    
    Page<Poem> findByCategory_Id(Long categoryId, Pageable pageable);
}